<?php

namespace Home\Controller;

use Home\Base\BaseController;

use Think\Controller;
use Think\Log;


//接口处
interface AdminInfoService
{
    public function index(); //查

    public function add();   //增

    public function edit();  //改

    public function selectAction();//增Ajax请求

    public function addAction();//增Ajax请求

    public function editAction();

    public function findAction();//查找某一条

    public function delAction();
}


class AdminInfo extends BaseController implements AdminInfoService
{

    //查询页面
    public function index()
    {
        $this->display();
    }

    //查询Ajax请求
    public function selectAction()
    {
        $where = [];
        $search_username = I('username');
        $search_admintype = I('admintype');
        $search_status = I('status');
        !empty($search_username) ? $where['username'] = $search_username : __DIR__;
        !empty($search_admintype) ? $where['admintype'] = $search_admintype : __DIR__;
        !empty($search_status) ? $where['status'] = $search_status : __DIR__;


        $AdminInfo = M('admininfo');
        $AdminInfos = $AdminInfo
            ->field("adminid, username, admintype, status")
            ->join('left join adminmoreinfo on adminmoreinfo.id=admininfo.id')
            ->join('left join admin_group on admin_group.admin_id=admininfo.id')
            ->where(['adminid' => ['gt' => '0']])
            ->where(['username' => ['like', " %{ $search_username}%"], 'admintype' => $search_admintype, 'status' => $search_status,])
            ->page($this->page, $this->limit)
            ->order(' adminid desc, admintype ')
            ->select();

        $count = $AdminInfo->where(['adminid' => ['gt' => '0']])->count();

        Log::write($AdminInfo->getLastSql());

        $this->AjaxSuccess(['AdminInfos' => $AdminInfos, 'count' => $count]);
    }


    //添加页面
    public function add()
    {
        $this->display();
    }

    //添加Ajax请求
    public function addAction()
    {
        #code
        $addData = [];
        $add_username = I('username');
        $add_admintype = I('admintype');
        $add_password = I('password');
        $add_status = I('status');
        !empty($add_username) ? $addData['username'] = $add_username : $this->AjaxError('用户名不能为空!');
        !empty($add_admintype) ? $addData['admintype'] = $add_admintype : $this->AjaxError('用户类型不能为空!');
        !empty($add_password) ? $addData['password'] = $add_password : $this->AjaxError('密码不能为空!');
        !empty($add_status) ? $addData['status'] = $add_status : $this->AjaxError('状态不能为空!');

        if (strlen($add_username) < 6) {
            $this->AjaxError(" 用户名不得小于6位! ");
        }
        if (strlen($add_admintype) < 6) {
            $this->AjaxError(" 用户类型不得小于6位! ");
        }
        if (strlen($add_password) < 6) {
            $this->AjaxError(" 密码不得小于6位! ");
        }
        if (strlen($add_status) < 1) {
            $this->AjaxError(" 状态不得小于1位! ");
        }


        $AdminInfo = M('admininfo');
        $isok = $AdminInfo
            ->data($addData)
            ->add();

        if (empty($isok)) {
            $this->AjaxError("添加失败!");
        } else {
            $this->AjaxSuccess("添加成功!");
        }


    }

    //查询页面
    public function edit()
    {
        $this->display();
    }

    //查询Ajax请求
    public function editAction()
    {
        #code

        $adminid = I('adminid');

        $addData = [];
        $add_username = I('username');
        $add_admintype = I('admintype');
        $add_password = I('password');
        $add_status = I('status');
        !empty($add_username) ? $addData['username'] = $add_username : $this->AjaxError('用户名不能为空!');
        !empty($add_admintype) ? $addData['admintype'] = $add_admintype : $this->AjaxError('用户类型不能为空!');
        !empty($add_password) ? $addData['password'] = $add_password : $this->AjaxError('密码不能为空!');
        !empty($add_status) ? $addData['status'] = $add_status : $this->AjaxError('状态不能为空!');

        if (strlen($add_username) < 6) {
            $this->AjaxError(" 用户名不得小于6位! ");
        }
        if (strlen($add_admintype) < 6) {
            $this->AjaxError(" 用户类型不得小于6位! ");
        }
        if (strlen($add_password) < 6) {
            $this->AjaxError(" 密码不得小于6位! ");
        }
        if (strlen($add_status) < 1) {
            $this->AjaxError(" 状态不得小于1位! ");
        }


        $AdminInfo = M('admininfo');
        $isok = $AdminInfo
            ->data($addData)
            ->where(['adminid' => $adminid])
            ->save();

        if (empty($isok)) {
            $this->AjaxError("修改失败!");
        } else {
            $this->AjaxSuccess("修改成功!");
        }

    }


    //删除Ajax
    public function delAction()
    {
        #code
        $adminid = I('adminid');
        $isok = M('admininfo')
            ->where(['adminid' => $adminid])
            ->delete();

        if (empty($isok)) {
            $this->AjaxError("删除失败!");
        } else {
            $this->AjaxSuccess("删除成功!");
        }
    }

    //查询单个Ajax请求
    public function findAction()
    {
        #code

        $adminid = I('adminid');

        $AdminInfo = M('admininfo');

        $AdminInfos = $AdminInfo
            ->where(['adminid' => $adminid])
            ->find();

        $this->AjaxSuccess($AdminInfos);
    }


}




